// pages/myPost/myPost.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:false,
    title:['我的帖子','我的点赞','我的出售'],
    idx:0,
    page:{
      p:0,
      psize:15
    },
    postArr:[],//我的帖子
    saleArr:[],//我的出售
    collectArr:[],//我的收藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  give(e){
    let id = e.currentTarget.dataset.postid,
      index = e.currentTarget.dataset.index;
    wx.request({
      url: base_url + '/Post/zan',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
        'post_id': id,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: res => {
        let arr = this.data.collectArr;
        if (res.data.success == '1') {
          if (arr[index].is_zan==0){
            arr[index].is_zan=1;
            arr[index].zan_num = parseInt(arr[index].zan_num)+1;
            wx.showToast({
              title: '点赞成功',
              icon: 'success',
              duration: 2000
            })
          }else{
            arr[index].zan_num = parseInt(arr[index].zan_num) - 1;
            arr[index].is_zan = 0;
            wx.showToast({
              title: '取消点赞',
              icon: 'success',
              duration: 2000
            })
          }
          this.setData({
            collectArr:arr
          })
        }
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      idx: options.idx
    });
    wx.setNavigationBarTitle({
      title: this.data.title[this.data.idx]
    })
    if (this.data.idx == 0) {
      this.postInit()
    } else if (this.data.idx == 1) {
      this.collectInit();
    } 
    else if(this.data.idx==3){
      this.dz();
    }
    else {
      this.saleInit()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let refresh = true;
    this.setData({
      'page.p':0
    })
    if(this.data.idx == 0){
      this.postInit(refresh)
    }else if(this.data.idx == 1){
      this.collectInit(refresh);
    }else{
      this.saleInit(refresh)
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      this.setData({
        'page.p':this.data.page.p + 1
      })
    if(this.data.idx == 0){
      this.postInit();
    } else if (this.data.idx == 1){
      this.collectInit();
    }else{
      this.saleInit();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  postInit(refresh = false){//我的帖子
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Users/getMyPost',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.page.p,
            'psize': this.data.page.psize
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            if (res.data.success == '1') {
              let postArr = (refresh ? res.data.data : this.data.postArr.concat(res.data.data));
              this.setData({
                postArr:postArr,
                loading:true
              })
              console.log(this.data.postArr)
            }
              wx.hideLoading();
              refresh == true && wx.stopPullDownRefresh();
          }
        })
      }
    })
  },
  saleInit(refresh = false){//我的收藏
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Users/getMySale',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.page.p,
            'psize': this.data.page.psize
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            if (res.data.success == '1') {
              let saleArr = (refresh ? res.data.data : this.data.saleArr.concat(res.data.data));
              this.setData({
                saleArr: saleArr,
                loading:true
              })
            }
            wx.hideLoading();
            refresh == true && wx.stopPullDownRefresh();
          }
        })
      }
    })
  },
  collectInit(refresh = false){//我的出售
  //Users/getMyCollect 
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Users/getZanList',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.page.p,
            'psize': this.data.page.psize
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            if (res.data.success == '1') {
              let collectArr = (refresh ? res.data.data : this.data.collectArr.concat(res.data.data));
              this.setData({
                collectArr: collectArr,
                loading: true
              })
            }
            wx.hideLoading();
            refresh == true && wx.stopPullDownRefresh();
          }
        })
      }
    })
  },
  go(e){
    let id = e.currentTarget.dataset.id;
  },
  want(e){

    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    console.log(type)
    if(type ==  10){
      wx.navigateTo({
        url: '../saledetail/saledetail?id='+id,
      })
    }else{
      wx.navigateTo({
        url: '../details/details?id='+id,
      })
    }
  }

})